﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;


namespace ftpd_console
{
    class FTP_Messages
    {

        //Послать сообщение по коду
        public static bool Send(NetworkStream stream, int code, string arg1 = null, string arg2 = null)
        {
            //Если поток существует
            if (stream != null)
            {
                try
                {
                    switch (code)
                    {
                        case 110:
                            Helpers.WriteLine(stream, code + " Restart marker only.");
                            break;
                        case 120:
                            Helpers.WriteLine(stream, code + " Service ready in " + arg1 + " minutes.");
                            break;
                        case 125:
                            Helpers.WriteLine(stream, code + " Data connections already open; transfer starting.");
                            break;
                        case 150:
                            Helpers.WriteLine(stream, code + " File status okay; about to open data connection.");
                            break;
                        case 200:
                            Helpers.WriteLine(stream, code + " Ok!");
                            break;
                        case 202:
                            Helpers.WriteLine(stream, code + " Command not implemented, superfluous at this site.");
                            break;
                        case 211:
                            Helpers.WriteLine(stream, code + "-Extensions supported");
                            Helpers.WriteLine(stream, code + "-UTF8");
                            //Helpers.WriteLine(stream, code + " 211");
                            Helpers.WriteLine(stream, code + " End of FEAT");
                            break;
                        case 214:
                            Helpers.WriteLine(stream, code + " The following commands are recognized and implemented");
                            //Helpers.WriteLine(stream, code + " The following commands are recognized and implemented");
                            //Helpers.WriteLine(stream, code + " The following commands are recognized and implemented");
                            //Helpers.WriteLine(stream, code + " The following commands are recognized and implemented");
                            break;
                        case 215:
                            Helpers.WriteLine(stream, code + " UNIX Type: L8");
                            break;
                        case 220:
                            Helpers.WriteLine(stream, code + " You're welcome! FTP-server by German Shutenko");
                            break;
                        case 221:
                            Helpers.WriteLine(stream, code + " Good bye!");
                            break;
                        case 226:
                            Helpers.WriteLine(stream, code + " Closing data connection.");
                            break;
                        case 227:
                            string ip = arg1.Replace('.', ',');
                            int port = Convert.ToInt32(arg2);
                            int port1 = port / 256;
                            int port2 = port - port1*256;
                            Helpers.WriteLine(stream, code + " Entering Passive Mode ("+ ip + ","+ port1 + "," + port2 +").");
                            break;
                        case 230:
                            Helpers.WriteLine(stream, code + " User "+arg1+" logged in");
                            break;
                        case 257:
                            Helpers.WriteLine(stream, code + " Current directory is: \"" + arg1 + "\"");
                            break;
                        case 331:
                            Helpers.WriteLine(stream, code + " Password required for "+arg1);
                            break;
                        case 425:
                            Helpers.WriteLine(stream, code + " Can't open data connection.");
                            break;
                        case 426:
                            Helpers.WriteLine(stream, code + " Connection closed; transfer aborted.");
                            break;
                        //case 431:
                        //    Helpers.WriteLine(stream, code + " No such directory \""+arg1+"\"");
                        //    break;
                        case 550:
                            Helpers.WriteLine(stream, code + " No such file or directory \"" + arg1 + "\"");
                            break;
                        case 500:
                            Helpers.WriteLine(stream, code + " Syntax error, command unrecognized.");
                            break;
                        case 501:
                            Helpers.WriteLine(stream, code + " Syntax error in parameters or arguments.");
                            break;
                        case 502:
                            Helpers.WriteLine(stream, code + " Command not implemented.");
                            break;
                        case 503:
                            Helpers.WriteLine(stream, code + " Bad sequence of commands.");
                            break;
                        case 530:
                            Helpers.WriteLine(stream, code + " Loggin incorrect.");
                            break;
                        case 553:
                            Helpers.WriteLine(stream, code + " Permission Denied");
                            break;
                        default:
                            return false;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }
            return true;
        }

        //Прочитать строку из потока
        public static string ReadLine(NetworkStream stream)
        {
            try
            {
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                string line = reader.ReadLine();
                return line;
            }
            catch (IOException e)
            {
                Console.WriteLine(e.StackTrace);
                return null;
            }
        }

    }
}
